from apps.system.article.baseViews import *


class ArticleForPViewSet(BaseArticleViewSet):
    authentication_classes = []
    permission_classes = []
    def get_queryset(self):
        _list = ArticleDao().findAll()
        return _list

    @action(methods=['post'], detail=False)
    def getArticlesByIds(self, request):
        params = getRequestParams(self.request)
        ids = params.get('ids')
        if type(ids) != list:
            return Response(response_message('ids参数格式错误'))
        _list = ArticleDao().findAll().filter(id__in=ids)
        return Response(response_success({'list': ArticleModelSerializer(_list, many=True).data}))

class ArticleViewSet(BaseArticleViewSet):

    def get_queryset(self):
        params = getRequestParams(self.request)
        _list = ArticleDao().findAll()
        return _list

    # 增加阅读次数
    @action(methods=['post'], detail=False)
    def addReadCount(self, request):
        params = getRequestParams(self.request)
        articleId = params.get('articleId')
        if not articleId:
            return Response(response_message('articleId参数必填'))
        article = ArticleDao().findOneByObjId(articleId)
        users = article.users.all().filter(user=request.user)
        if not users.exists():
            article.users.add(*[request.user.id])
            article.save()
        return Response(response_message('操作成功'))